package edu.scau.DAO_mode.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.HeadlessException;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JTextField;
import javax.swing.JPasswordField;

import org.nutz.dao.entity.annotation.Table;

import edu.scau.DAO_mode.model.User;
import edu.scau.DAO_mode.serviceimp.UserServiceImp;
import edu.scau.DAO_mode.util.DatabaseConnection;
import edu.scau.DAO_mode.util.Error;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

public class LoginDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField userNameField;
	private JPasswordField passwordField;
	/**
	 * Launch the application.
	 * 
		 */
	  public static void main(String[] args) { try { LoginDialog dialog = new
	  LoginDialog(); dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
	  dialog.setVisible(true); } catch (Exception e) { e.printStackTrace(); } }


	/**
	 * Create the dialog.
	 */
	public LoginDialog() {
		setTitle("登陆");
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);

		JLabel label = new JLabel("用户名");
		label.setFont(new Font("宋体", Font.PLAIN, 20));
		label.setBounds(84, 58, 81, 46);
		contentPanel.add(label);

		userNameField = new JTextField();
		userNameField.addFocusListener(new FocusAdapter() {
			public void focusLost(FocusEvent arg0) {
				UserServiceImp userServiceImp = null;
				try {
					userServiceImp=new UserServiceImp();
					if (userServiceImp.findUser(userNameField.getText())==false) {   //用户名不存在
						JOptionPane.showMessageDialog(null, Error.Return(-1));
					}
				} catch (HeadlessException e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
				} catch (Exception e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
				}				
			}
		});
		userNameField.setBounds(175, 73, 109, 21);
		contentPanel.add(userNameField);
		userNameField.setColumns(10);

		JLabel label_1 = new JLabel("密码");
		label_1.setFont(new Font("宋体", Font.PLAIN, 20));
		label_1.setBounds(84, 124, 81, 46);
		contentPanel.add(label_1);

		passwordField = new JPasswordField();
		passwordField.setBounds(175, 139, 109, 21);
		contentPanel.add(passwordField);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton loginButton = new JButton("登陆"); //调用service类进行登陆
				loginButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						UserServiceImp userServiceImp = null;
						try {
							userServiceImp = new UserServiceImp();
							if (userServiceImp.login(userNameField.getText(),passwordField.getText())) 
							{
								MainFrame mainFrame=new MainFrame();
								mainFrame.setVisible(true);
								dispose();
							}
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				});

				JButton registeButton = new JButton("注册");
				registeButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						RegisterDialog registerDialog=new RegisterDialog();
						registerDialog.setVisible(true);
						setFocusable(false);
					}
				});
				registeButton.setActionCommand("OK");
				buttonPane.add(registeButton);
				loginButton.setActionCommand("OK");
				buttonPane.add(loginButton);
				getRootPane().setDefaultButton(loginButton);
			}
			{
				JButton cancelButton = new JButton("取消");
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
}
